/**
 * 时间格式转换
 * @param strTime 要转换的时间戳
 */
 const formatDate = (strTime) => {
  let date = strTime ? new Date(strTime) : new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return year + '/' + month + '/' + day
}
/**
 * 星期
 * @param strTime 要转换的时间戳
 */
const getDay = (strTime) => {
  let date = strTime ? new Date(strTime) : new Date()
  let day = date.getDay()
  const weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]
  return weekday[day]
}
const setStore = (name, content) => {
  if (!name) return
  if (typeof content !== 'string') {
    content = JSON.stringify(content)
  }
  window.localStorage.setItem(name, content)
}
// 获取存取
const getStore = (name) => {
  if (!name) return
  return window.localStorage.getItem(name)
}
const handleDataCon2 = (arr) => {
  let alarmTime = getStore('alarmTime')
  let lockTime = getStore('lockTime')
  let result = ''
  if (Number(arr) === 0) {
    result = { name: 'kong'}
    return result
  }
  
  let len = arr && arr.length

  for(let i = 0; i< arr.length; i++){
    if(arr[i]['name'] === 'connectionStatus'){
      if(arr[i]['value'] === '0') {
        result = arr[i]
        break
      }
    }
  }
  if(result) { return result}

  for(let i = 0; i< arr.length; i++){
    if(arr[i]['name'] === 'dh.alarmType'){
      if(!alarmTime ||Number(alarmTime) !== arr[i]['lastUpdateTimeMs']) {
        setStore('alarmTime',arr[i]['lastUpdateTimeMs'])
        result = arr[i]
        break
      }
    }
  }
  if(result) { return result}
  for(let i = 0; i< len; i++)	{
    if(arr[i]['name'] === 'unlockWay'){
      if(!lockTime || Number(lockTime) !== arr[i]['lastUpdateTimeMs']){
        setStore('lockTime',arr[i]['lastUpdateTimeMs'])        
        result = arr[i]
      } else {
        result = { name: 'kong'}
      }
      break;
    }
  }
  console.log(result)
  return result
}
/**
 * 数据处理 返回满足优先条件 低压 报警 正常
 */
const handleDataCon = (arr) => {
  console.log(arr)
  let result = ''
  if (Number(arr) === 0) {
    result = { name: 'kong'}
    return result
  }
  
  let len = arr && arr.length
  for(let i = 0; i< arr.length; i++){
    if(arr[i]['name'] === 'dh.alarmType' && arr[i]['value'] === '2'){
      result = arr[i]
      break
    }
  }
  if(result) { return result}
  for(let i = 0; i< len; i++)	{
      if(arr[i]['name'] === 'dh.alarmType' && arr[i]['value'] !== '2'){
        result = arr[i]
        break
      }
    }
  if(result) { return result}
  for(let i = 0; i< len; i++)	{
    if(arr[i]['name'] === 'unlockWay'){
      result = arr[i]
      break
    }
  }
  console.log(result)
  return result
}
/**
 * 展示tips框
 */
  // let tipsCon = document.querySelector('.tips-content')
  // let mask = document.querySelector('.mask')
  const Tips = {
    tipsCon : document.querySelector('.tips-content'),
    mask : document.querySelector('.mask'),
    showTips: function () {
      this.tipsCon.style.display = 'block'
      this.mask.style.display = 'block'
    },
    hiddenTips: function () {
      this.tipsCon.style.display = 'none'
      this.mask.style.display = 'none'
    }
  }
/**
 * template 注册方法
 */
// 时间格式化
template.helper('formatDate', function (onedate = true, strTime) {
  let date = strTime ? new Date(strTime) : new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  let result = ''
  if (onedate) {
    result = year + '/' + month + '/' + day
  } else {
    result = hours + ':' + minutes + ':' + seconds
  }
  return result
})
// 星期
template.helper('getDay', function(strTime) {
  let date = strTime ? new Date(strTime) : new Date()
  let day = date.getDay()
  const weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]
  return weekday[day]
})
// 过滤器 参数名 和参数值对应的实际文字
template.helper('filterParamName', function(key, value){
  let result = ''
  let PValue = parseInt(value)
  const lock = ['密码', '指纹', '射频卡']
  const alarm = ['非法用户（非法尝试开门）', '防拆报警', '低电压报警']
  // 传入的值为 功能名 例如 unlockWay 对应的值 0,1,2 unlockWay为功能名, 0,1,2为功能值
  if (key === lockWay) {
    result = '开锁方式' + lock[PValue]
  } else if (key === alarmType) {
    result = '报警类型' + alarm[PValue]
  }
  return result;
})

// 加载图标
let loaderEle = document.getElementById('loader');
function startLoading() {
  console.log('加载')
    loaderEle.setAttribute('class', '');
}
function stopLoading() {
    loaderEle.setAttribute('class', 'hide');
}

// 提示 toast
let toastEle = document.getElementById('toast')
function showToast (text) {
  let  toast = document.getElementsByClassName('toast-hook')[0]
  toastEle.setAttribute('class', '');
  toast.innerHTML = text
  console.log(toast)
  
  setTimeout(function () {
    hideToast()
  }, 2000)
}
function hideToast () {
  toastEle.setAttribute('class', 'hide');
}
